//
//  XMClassViewController.m
//  Second
//
//  Created by Alex on 16/7/13.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "XMClassViewController.h"

@interface XMClassViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@end

@implementation XMClassViewController

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        //设置item的大小
        layOut.itemSize     = CGSizeMake(kWidth/2-10, 100);
        //设置分区上下左右距离
        layOut.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        //区头大小
        layOut.headerReferenceSize = CGSizeMake(kWidth, 30);
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layOut];
        _collectionView.delegate   = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor grayColor];
        
    }
    return _collectionView;
}
//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
//每个分区item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 6;
    }else{
        return 4;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationItem.title = @"分类";
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"class_cell"];
    //注册区头
    [self.collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"Header"];
    //注册自定义的cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"ClassCell" bundle:nil] forCellWithReuseIdentifier:@"class_cell"];
    
}



//cell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"class_cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];

    if (indexPath.section == 0) {
                if (indexPath.row == 0) {
            [cell setCellimageName:@"26.jpg" title:@"手机" list:@"苹果 华为"];
        }
        if (indexPath.row == 1) {
            [cell setCellimageName:@"26.jpg" title:@"数码" list:@"电脑 相机"];
        }
        if (indexPath.row == 2) {
            [cell setCellimageName:@"26.jpg" title:@"运动器材" list:@"篮球 哑铃"];
        }
        if (indexPath.row == 3) {
            [cell setCellimageName:@"26.jpg" title:@"服饰" list:@"衣服 鞋子"];
        }
        if (indexPath.row == 4) {
            [cell setCellimageName:@"26.jpg" title:@"图书教材" list:@"课本 笔记"];
        }
        if (indexPath.row == 5) {
            [cell setCellimageName:@"26.jpg" title:@"代步工具" list:@"自行车 电动车 "];
        }
        
        return cell;
    }
    if (indexPath.section == 1 ) {
        if (indexPath.row == 0) {
        [cell setCellimageName:@"26.jpg" title:@"代跑腿" list:@"取快递 送外卖"];
        }
        if (indexPath.row == 1) {
            [cell setCellimageName:@"26.jpg" title:@"辅导课程" list:@"期末冲关 考研"];
        }
        if (indexPath.row == 2) {
            [cell setCellimageName:@"26.jpg" title:@"陪玩" list:@"打游戏 打球"];
        }
    return cell;
    }
    return nil;
}

//返回区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
    //设置区头的类容
    if (indexPath.section == 0) {
        headerView.label.text = @"二手市场";
    }else{
        headerView.label.text = @"一技之长";
    }
    
    return headerView;
}
//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ClassListViewController *classListVC = [[ClassListViewController alloc]init];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            classListVC.typeID = @2;
        }
        if (indexPath.row == 1) {
           classListVC.typeID = @4;
        }
        if (indexPath.row == 2) {
            classListVC.typeID = @8;
        }
        if (indexPath.row == 3) {
            classListVC.typeID = @6;
        }
        if (indexPath.row == 4) {
           classListVC.typeID = @7;
        }
        if (indexPath.row == 5) {
            classListVC.typeID = @1;
        }
    
    }
    

    [self.navigationController pushViewController:classListVC animated:YES];
     
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
   
}


@end
